/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mti.javashare.webservices;

import com.mti.javashare.beans.File;
import com.mti.javashare.dao.IFileDao;
import com.mti.javashare.dao.IUserDao;
import java.io.*;
import java.util.Collection;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author Toto
 */
@WebService(endpointInterface = "com.mti.javashare.webservices.IFileWebService")
public class FileWebService implements IFileWebService
{
    @Autowired
    private IUserDao userDao;
    
    @Autowired
    private IFileDao fileDao;
    
    @Override
    public File getById(int id) 
    {
        return fileDao.getById(id);
    }

    @Override
    public File getByName(String name)
    {
        return fileDao.getByName(name);
    }

    @Override
    public Collection<File> getAll()
    {
        return fileDao.getAll();
    }
    
    @Override
    public String add(int iduser, String filename, String description, InputStream file)
    {
        // Set a system environment variable named JAVASHARE_HOME to the directory where you want to store the uploaded files.
        // You will probably need to restart your IDE/server/whatever before it takes effect.
        String path = System.getenv("JAVASHARE_HOME") + "/Upload/" + filename;
        
        OutputStream out;
        
        try 
        {
            out = new FileOutputStream(new java.io.File(path));
        }
        catch (FileNotFoundException ex)
        {
            return ex.getMessage();
        }
            
        int read;
        byte[] bytes = new byte[1024];
        
        try
        {
            while ((read = file.read(bytes)) != -1)
            {
                out.write(bytes, 0, read);
            }

            file.close();
            out.flush();
            out.close();
        }
        catch (IOException ex)
        {
            return ex.getMessage();
        }
        
        File fileBean = new File();
        fileBean.setName(filename);
        fileBean.setDescription(description);
        fileBean.setUser(userDao.getById(iduser));
        fileDao.add(fileBean);
        
        return "Success";
    }

    @Override
    public java.io.File downloadById(int id)
    {
        File fileBean = fileDao.getById(id);
        String path = System.getenv("JAVASHARE_HOME") + "/Upload/" + fileBean.getName();
        java.io.File file = new java.io.File(path);
        return file;
    }

    @Override
    public java.io.File download(String name)
    {
        File fileBean = fileDao.getByName(name);
        String path = System.getenv("JAVASHARE_HOME") + "/Upload/" + fileBean.getName();
        java.io.File file = new java.io.File(path);
        return file;
    }

    @Override
    public void update(int id, String name, String description, int iduser)
    {
        File fileBean = fileDao.getById(id);
        
        String path = System.getenv("JAVASHARE_HOME") + "/Upload/" + fileBean.getName();
        String newPath = System.getenv("JAVASHARE_HOME") + "/Upload/" + name;
        java.io.File file = new java.io.File(path);
        file.renameTo(new java.io.File(newPath));
        
        fileBean.setName(name);
        fileBean.setDescription(description);
        fileBean.setUser(userDao.getById(iduser));
        fileDao.update(fileBean);
    }

    @Override
    public void remove(int id)
    {
        File fileBean = fileDao.getById(id);
        
        String path = System.getenv("JAVASHARE_HOME") + "/Upload/" + fileBean.getName();
        java.io.File file = new java.io.File(path);
        file.delete();
        
        fileDao.remove(fileBean);
    }
    
}
